/**
 * Copyright © 2021-2025 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.resource.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.jeeplus.aop.logging.annotation.ApiLog;
import com.jeeplus.core.excel.EasyExcelUtils;
import com.jeeplus.core.excel.ExcelOptions;
import com.jeeplus.core.excel.annotation.ExportMode;
import com.jeeplus.core.query.QueryWrapperGenerator;
import com.jeeplus.resource.domain.Venu;
import com.jeeplus.resource.service.VenuService;
import com.jeeplus.resource.service.VenueStatusService;
import com.jeeplus.resource.service.dto.VenuDTO;
import com.jeeplus.resource.service.dto.VenueStatusDTO;
import com.jeeplus.resource.service.mapstruct.VenuWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

/**
 * 场地Controller
 * @author 淋漓
 * @version 2024-08-24
 */

@Api(tags ="场地")
@RestController
@RequestMapping(value = "/resource/venu")
public class VenuController {

	@Autowired
	private VenuService venuService;

	@Autowired
	private VenuWrapper venuWrapper;

	@Autowired
	private VenueStatusService venueStatusService;

	/**
	 * 场地列表数据
	 */
	@ApiLog("查询场地列表数据")
	@ApiOperation(value = "查询场地列表数据")
	@PreAuthorize("hasAuthority('resource:venu:list')")
	@GetMapping("list")
	public ResponseEntity<IPage<VenuDTO>> list(VenuDTO venuDTO, Page<Venu> page) throws Exception {
		QueryWrapper queryWrapper = QueryWrapperGenerator.buildQueryCondition (venuDTO, VenuDTO.class);
		IPage<VenuDTO> result = venuWrapper.toDTO ( venuService.page (page, queryWrapper) );
		return ResponseEntity.ok (result);
	}


	/**
	 * 根据Id获取场地数据
	 */
	@ApiLog("根据Id获取场地数据")
	@ApiOperation(value = "根据Id获取场地数据")
	@PreAuthorize("hasAnyAuthority('resource:venu:view','resource:venu:add','resource:venu:edit')")
	@GetMapping("queryById")
	public ResponseEntity<VenuDTO> queryById(String id) {
		return ResponseEntity.ok ( venuWrapper.toDTO ( venuService.getById ( id ) ) );
	}

	/**
	 * 保存场地
	 */
	@ApiLog("保存场地")
	@ApiOperation(value = "保存场地")
	@PreAuthorize("hasAnyAuthority('resource:venu:add','resource:venu:edit')")
	@PostMapping("save")
	@Transactional
	public  ResponseEntity <String> save(@Valid @RequestBody VenuDTO venuDTO) {
		//新增或编辑表单保存
		venuService.saveOrUpdate (venuWrapper.toEntity (venuDTO));
		venuDTO.setId(venuService.getOne(new QueryWrapper<Venu>().eq("venue_name", venuDTO.getVenueName())).getId());
		venueStatusService.saveBatch(VenueStatusDTO.getVenueStatusList(venuDTO));
        return ResponseEntity.ok ( "保存场地成功" );
	}


	/**
	 * 删除场地
	 */
	@ApiLog("删除场地")
	@ApiOperation(value = "删除场地")
	@PreAuthorize("hasAuthority('resource:venu:del')")
	@DeleteMapping("delete")
	public ResponseEntity <String> delete(String ids) {
		String idArray[] = ids.split(",");
        venuService.removeByIds ( Lists.newArrayList ( idArray ) );
		return ResponseEntity.ok( "删除场地成功" );
	}

    /**
     * 导出场地数据
     *
     * @param venuDTO
     * @param page
     * @param response
     * @throws Exception
     */
    @ApiLog("导出场地数据")
    @PreAuthorize("hasAnyAuthority('resource:venu:export')")
    @GetMapping("export")
    public void exportFile(VenuDTO venuDTO, Page <Venu> page, ExcelOptions options, HttpServletResponse response) throws Exception {
        String fileName = options.getFilename ( );
		QueryWrapper queryWrapper = QueryWrapperGenerator.buildQueryCondition (venuDTO, VenuDTO.class);
        if ( ExportMode.current.equals ( options.getMode ( ) ) ) { // 导出当前页数据

        } else if ( ExportMode.selected.equals ( options.getMode ( ) ) ) { // 导出选中数据
            queryWrapper.in ( "id", options.getSelectIds () );
        } else { // 导出全部数据
            page.setSize ( -1 );
            page.setCurrent ( 0 );
        }
        List < Venu> result = venuService.page ( page, queryWrapper ).getRecords ( );
        EasyExcelUtils.newInstance ( venuService, venuWrapper ).exportExcel ( result,  options.getSheetName ( ), VenuDTO.class, fileName,options.getExportFields (), response );
    }

    /**
     * 导入场地数据
     *
     * @return
     */
    @PreAuthorize("hasAnyAuthority('resource:venu:import')")
    @PostMapping("import")
    public ResponseEntity importFile(MultipartFile file) throws IOException {
        String result = EasyExcelUtils.newInstance ( venuService, venuWrapper ).importExcel ( file, VenuDTO.class );
        return ResponseEntity.ok ( result );
    }

    /**
     * 下载导入场地数据模板
     *
     * @param response
     * @return
     */
    @PreAuthorize ("hasAnyAuthority('resource:venu:import')")
    @GetMapping("import/template")
    public void importFileTemplate(HttpServletResponse response) throws IOException {
        String fileName = "场地数据导入模板.xlsx";
        List<VenuDTO> list = Lists.newArrayList();
        EasyExcelUtils.newInstance ( venuService, venuWrapper ).exportExcel ( list,  "场地数据", VenuDTO.class, fileName, null, response );
    }


}
